package com.jn.erp.wechat.student.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.web.api.interaction.entity.TEvaluate;
import com.jn.erp.web.api.interaction.entity.TIntegral;
import com.jn.erp.web.api.interaction.entity.TIntegralConfig;
import com.jn.erp.web.api.interaction.service.TEvaluateService;
import com.jn.erp.web.api.interaction.service.TIntegralConfigService;
import com.jn.erp.web.api.interaction.service.TIntegralService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import com.jn.erp.wechat.core.WxTableDataInfo;
import com.jn.erp.wechat.student.api.entity.vo.ClassInfoVo;
import com.jn.erp.wechat.student.api.entity.vo.StudentEvaluatedVo;
import com.jn.erp.wechat.student.api.service.ClassInfoService;
import com.jn.erp.wechat.student.api.service.StudentEvaluatedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <pre>
 *  课后服务-评价老师
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/11/11 13:53
 */
@RestController
@RequestMapping("/wxs/eva")
public class WxsStudentEvaluationTeacherController extends BaseWxAuthController {

    @Autowired
    private ClassInfoService classInfoService;

    @Autowired
    private TEvaluateService evaluateService;

    @Autowired
    private StudentEvaluatedService studentEvaluatedService;

    @Autowired
    private TIntegralService tIntegralService;

    @Autowired
    private TIntegralConfigService tIntegralConfigService;

    //待评价 列表 (已点名课次)
    @GetMapping("/notEvaluatedList")
    public WxTableDataInfo notEvaluatedList(){
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();

        ClassInfoVo classInfoVo=new ClassInfoVo();
        classInfoVo.setUserId(studentUser.getUserId());
        classInfoVo.setEvaType("1");
        startPage();
        List<ClassInfoVo> list = classInfoService.orderDateList(classInfoVo);
        return getWxDataTable(list);
    }

    /**
     * 保存评价
     * tId 教师id  orderNo 课次编号  description 评价内容  tState 份数 evaluator  学员id evaluator  学员id emplateId 课次id
     */

    @PostMapping("saveEvaluated")
    @Transactional
    public AjaxResult saveEvaluated(@RequestBody TEvaluate evaluate){
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        evaluate.setType(2);//评价老师
        evaluate.setEvaluator(evaluate.getEvaluator());//评价人
        evaluate.setCreateDate(LocalDateTime.now());
        evaluate.setCreateUser(studentUser.getUserId()+"");
        evaluate.setState(1);
        evaluate.setIsDelete("0");
        boolean save = evaluateService.save(evaluate);
        if (save) {
            //添加积分
            TIntegralConfig t = tIntegralConfigService.getOne(new QueryWrapper<TIntegralConfig>().eq("org_id", evaluate.getOrgId()).eq("is_delete", "0"));
            if (!Objects.isNull(t)) {
                TIntegral tIntegral = new TIntegral(evaluate.getOrgId(),evaluate.getEvaluator(), t.getId(), 3, t.getAttr3(), t.getName(), "评价", LocalDateTime.now(), studentUser.getUserId()+"", LocalDateTime.now(), studentUser.getUserId()+"", "0");
                tIntegralService.save(tIntegral);
            }
        }
        return success();
    }

    //评价列表
    @GetMapping("/evaluatedList")
    public WxTableDataInfo evaluatedList(){
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        StudentEvaluatedVo studentEvaluatedVo=new StudentEvaluatedVo();
        studentEvaluatedVo.setUserId(studentUser.getUserId());
        startPage();
        List<StudentEvaluatedVo> list =  studentEvaluatedService.evaluatedList(studentEvaluatedVo);
        return getWxDataTable(list);
    }
//
//    //获取评价
//    @PostMapping("getEvaluated")
//    public AjaxResult getEvaluated(@RequestBody TEvaluate evaluate){
//        //获取登录家长端相关信息
//        WxUserDto studentUser = getStudentUser();
//        TEvaluate byId = evaluateService.getById(evaluate.getId());
//        return success(byId);
//    }

}
